package ru.xeden.web.internal;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.xeden.web.controller.IController;
import ru.xeden.web.controller.IControllerRegisterService;
import ru.xeden.web.html.ContentType;
import ru.xeden.web.html.RenderContext;
import ru.xeden.web.html.dom.DefaultMarkupModel;
import ru.xeden.web.html.module.UrlPieces;
import ru.xeden.web.html.module.internal.RootModule;
import ru.xeden.web.html.page.IPage;
import ru.xeden.web.model.ModelAndView;
import ru.xeden.web.request.IRequest;
import ru.xeden.web.request.IResponse;
import ru.xeden.web.request.internal.RequestImpl;
import ru.xeden.web.request.internal.ResponseImpl;
import ru.xeden.web.view.IViewRegisterService;


public class XedenServlet
    extends HttpServlet
{
    private static final Logger LOGGER = LoggerFactory.getLogger(XedenServlet.class);
    private String encoding = "UTF-8";
    private RootModule rootModule;
    private IControllerRegisterService controllerRegister;
    private IViewRegisterService viewRegister;
    
    public void setRootModule(RootModule rootModule)
    {
        this.rootModule = rootModule;
    }
    
    public void setControllerRegister(IControllerRegisterService controllerRegister)
    {
        this.controllerRegister = controllerRegister;
    }

    public void setViewRegister(IViewRegisterService viewRegister)
    {
        this.viewRegister = viewRegister;
    }
    
    protected void doGet(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException
    {
        LOGGER.debug("vvv--- START REQUEST--- " +  servletRequest.getRequestURI());
        long startTime = System.currentTimeMillis();
        IRequest request = new RequestImpl(servletRequest, encoding);
        IResponse response = new ResponseImpl(servletResponse, encoding);
        List<IController> controllers = controllerRegister.getControllers();
        ModelAndView modelAndView = new ModelAndView();
        for (IController controller : controllers)
        {
            if (controller.handle(request, response, modelAndView))
            {
                String viewId = modelAndView.getViewId();
                viewRegister.getViewById(viewId).render(response.getOutputStream(ContentType.TEXT_HTML.getContentType()), modelAndView);
                break;
            }
        }
        
        

//        
//        
//        PrintWriter printWriter = null;
//        try
//        {
//            UrlPieces urlPieces = new UrlPieces(request);
//            IPage page = rootModule.getPage(request, urlPieces);
//            if (page == null)
//            {
//                new DefaultErrorHandler().handleNotFound(servletRequest, servletResponse);
//                return;
//            }
//            printWriter = response.getPrintWriter(ContentType.TEXT_HTML.getContentType());
//            RenderContext markupContext = new RenderContext(new DefaultMarkupModel(), request, response);
//            page.toMarkup(markupContext, printWriter);
//        }
//        catch (IOException e)
//        {
//            new DefaultErrorHandler().handle(servletRequest, servletResponse, e);
//        }
//        finally
//        {
//            if (printWriter != null)
//            {
//                printWriter.close();
//            }
//            
//        }
        LOGGER.debug("^^^-------- END REQUEST --------- " + (System.currentTimeMillis() - startTime)  + "ms");
    }
}
